<?
    require_once("config.php");
    
    $adminmenu = new Menu("FAQ Administration");
    $adminmenu->AddItem(new MenuItem("Back to FAQ","faq.php",AuthConstants::ADMINISTRATORS));
    $adminmenu->AddItem(new MenuItem("Question Index","faq.php?action=list",AuthConstants::ADMINISTRATORS));
    $adminmenu->AddItem(new MenuItem("Edit Categories","faqcategories.php",AuthConstants::ADMINISTRATORS));
    $adminmenu->AddItem(new MenuItem("Add a Question","faq.php?action=edit",AuthConstants::ADMINISTRATORS));
    
    if ($_POST) {
        $build = array();
        $build['question'] = $_POST['question'];
        $build['answer'] = $_POST['answer'];
        $build['categoryid'] = $_POST['category'];
        
        if ($_POST['id'])
            $query = QueryTools::CreateQuery($build,"faq","update",$_POST['id']); // update
        else
            $query = QueryTools::CreateQuery($build,"faq"); // insert
            
        DBM::Query($query);
        header("Location: faq.php?action=list");
    }
    else if ($_GET['action'] == "list") {
        MenuManager::AddMenu("FAQ Administration",$adminmenu);
        Functions::PrintHeader("FAQ Administration");
        if ($_GET['deleted'] == 1) SuccessMessage::Show("Question Deleted","The question was successfully deleted");
        $faqs = DBM::FetchAllRows("SELECT *,faq.id as faqid FROM faq JOIN faq_categories ON faq.categoryid=faq_categories.id " . ($_GET['category'] ? "WHERE faq.categoryid=$_GET[category] " : "") . "ORDER BY faq.categoryid,question;");
        
        if (count($faqs) == 0) WarningMessage::Show("No FAQs","There are no Frequently Asked Questions in the database...you should probably create some.");
        else {
        ?>
        <table class='wide cgltable'>
            <tr>
                <th>Question</th>
                <th style='width:150px;'>Category</th>
                <th style='width:150px;'>Actions</th>
            </tr>
            <?
                $truncateat = 50;
                foreach ($faqs as $faq) {
                    echo "<tr>";
                    echo "<td>" . substr($faq['question'],0,$truncateat) . (strlen($faq['question']) > $truncateat ? "..." : "") . "</td>";
                    echo "<td>$faq[categoryname]</td>";
                    echo "<td><a href='faq.php?action=edit&id=$faq[faqid]'>edit</a> | <a href='faq.php?action=delete&id=$faq[faqid]'>delete</a></td>";
                    echo "</tr>";
                }
            ?>
        </table>
        <?
        }
        Functions::PrintFooter();
    }
    else if ($_GET['action'] == "edit") {
        $action = ($_GET['id'] ? "Edit" : "Add");
        MenuManager::AddMenu("$action FAQ",$adminmenu);
        Functions::PrintHeader("$action FAQ");
        ?>
        <form method="POST" action="faq.php">
            <?  
                $info = array();
                if ($_GET['id']) {
                    $info = DBM::FetchRow("SELECT * FROM faq WHERE id=$_GET[id];");
                    echo "<input type='hidden' name='id' value='$_GET[id]' />\n";
                }
            ?>
            <table class='cgltable' align='center'>
                <tr>
                    <th colspan='2'><?=$action?> FAQ</th>
                </tr>
                <tr>
                    <td class='label'>Category</td>
                    <td><select name='category'>
                    <?
                        $categories = DBM::FetchAllRows("SELECT * FROM faq_categories ORDER BY categoryname ASC;");
                        foreach ($categories as $category) {
                            echo "<option value='$category[id]'" . ($category['id'] == $info['categoryid'] ? " selected" : "") . ">$category[categoryname]</option>";
                        }
                    ?></select>
                    </td>
                </tr>
                <tr>
                    <td class='label'>Question</td>
                    <td><input type='text' size='80' name='question' value='<?=$info['question']?>' /></td>
                </tr>
                <tr>
                    <td class='label'>Answer</td>
                    <td><textarea rows='10' class='wide' name='answer'><?=$info['answer']?></textarea></td>
                </tr>
                <tr>
                    <td colspan='2'><input type='reset' value='Start Over' /><input type='submit' value='Save FAQ' /></td>
                </tr>
            </table>
        </form>
        <?
        Functions::PrintFooter();
    }
    else if ($_GET['action'] == "delete") {
        DBM::Query("DELETE FROM faq WHERE id=$_GET[id];");
        header("Location: faq.php?action=list&deleted=1");
    }
    else if (isset($_GET['category'])) {
        Functions::PrintHeader("FAQ");
        $faqs = DBM::FetchAllRows("SELECT * FROM faq JOIN faq_categories ON faq.categoryid=faq_categories.id WHERE categoryid=$_GET[category] ORDER BY faq.categoryid DESC");
        $lastcategory = $faqs[0]['categoryid'];
        echo "<h2>" . $faqs[0]['categoryname'] . "</h2>";
        echo "<div style='padding-left:25px;'>";
        foreach ($faqs as $faq) {
            if ($faq['categoryid'] != $lastcategory) {
                echo "</div>";
                echo "<br />";
                echo "<a name='$faq[categoryid]'></a>";
                echo "<h2>$faq[categoryname]</h2>";
                echo "<div style='padding-left:25px;'>";
                $lastcategory = $faq['categoryid'];
            }
            echo "<span class='faq_question'>" . stripslashes($faq['question']) . "</span>";
            echo "<div class='faq_answer'>" . stripslashes($faq['answer']) . "</div>";
        }
        echo "</div>";
        Functions::PrintFooter();
    }
    else {
        $categories = DBM::FetchAllRows("SELECT * FROM faq_categories ORDER BY categoryname,id ASC;");
        $menu = new Menu("FAQ Categories");
        foreach ($categories as $category) {
            $menu->AddItem(new MenuItem($category['categoryname'],'#' . $category['id']));
        }
        $menu->AddItem(new MenuItem("","#",AuthConstants::ADMINISTRATORS)); // an empty line
        $menu->AddItem(new MenuItem("","#",AuthConstants::ADMINISTRATORS)); // an empty line
        $menu->AddItem(new MenuItem("Edit Questions","faq.php?action=list",AuthConstants::ADMINISTRATORS));
        $menu->AddItem(new MenuItem("Add Question","faq.php?action=edit",AuthConstants::ADMINISTRATORS));
        $menu->AddItem(new MenuItem("","#",AuthConstants::ADMINISTRATORS)); // an empty line
        $menu->AddItem(new MenuItem("Edit Categories","faqcategories.php",AuthConstants::ADMINISTRATORS));
        $menu->AddItem(new MenuItem("Add Category","faqcategories.php?action=edit",AuthConstants::ADMINISTRATORS));
        MenuManager::AddMenu("FAQ",$menu);
        
        Functions::PrintHeader("FAQ");
        $query = "SELECT * FROM faq_categories ORDER BY categoryname ASC;";
        foreach (DBM::FetchAllRows($query) as $category) {
            echo "<h2><a href='faq.php?category=$category[id]'>$category[categoryname]</a></h2>";
        }
        Functions::PrintFooter();
    }
?>
